package com.example.aaa_viewpager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.example.aaa_viewpager.entity.User;
import com.example.aaa_viewpager.entity.UserCustom;
import com.example.aaa_viewpager.service.UserCustomDao;
import com.example.aaa_viewpager.service.UserDAO;

import android.R;
import android.R.bool;
import android.R.integer;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.provider.CalendarContract.Colors;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class BB_details extends Activity 
{
	// UI
	private ImageView bb_img;
	private TextView bb_name;
	private TextView bb_address;
	private TextView bb_phone;
	private TextView bb_price1;
	private TextView bb_price2;
	private TextView bb_price3;
	private Button phone_btn;
	private Button favorite_btn;
	private ImageButton map_btn;

	// data
	private UserDAO userDao;
	private List<User> bb_list;

	// userCustom data
	private UserCustomDao userCustomDao;
	private UserCustom userCustom;

	// 從N_Activity傳過來的資訊
	private int position;
	

	// 使用者自訂的資料庫 新增的資料
	private String orderName;
	private String orderAddress;
	private byte[] orderPic;

	// 判斷使用者自訂的資料庫 是否有資料寫入
	private boolean value;

	// Attribute
	private String name;
	private String address;
	private String phone;
	private int price_1;
	private int price_2;
	private int price_3;
	private Bitmap bit;

	public static final String TAG = "BUG";


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bb_details);
		//限制應用程式只能直立，禁止橫向
		setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );
		initView();
		initHandler();
	}

	private void initHandler() {
		phone_btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// /給予"通話鍵"可以直接撥打上頁選取之民宿的電話
				Intent it = new Intent();
				it.setAction("android.intent.action.CALL");
				String msg = userDao.getPhone(position).toString();
				String phone_number = "tel:" + msg;
				it.setData(Uri.parse(phone_number));
				// it.setData(Uri.parse("tel:0225149191"));
				startActivity(it);
			}
		});
		myFavorite();
		watchMap();

	}
	
	private void watchMap() 
	{
		
		map_btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				
				//String str = "http://card.url.com.tw/realads/map_latlng.php?pos=" + "經緯度";
				String bb_geo = userDao.getGeo(position);
				String str = "geo:"+ bb_geo; 
				Uri uri = Uri.parse(str);
				Intent it = new Intent(Intent.ACTION_VIEW,uri);
				startActivity(it);
				

			}
		});
		
		
	}

	//使用者收藏民宿功能
	private void myFavorite() {
		favorite_btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				// orderName = userDao.getName(position);
				
				orderAddress = userDao.getAddress(position);
				orderPic = userDao.getPicByte(position);

				userCustom = new UserCustom(position, orderName, orderAddress,orderPic);

				// value = userCustomDao.getUserById(orderName);

				if (value)
				{
					userCustomDao.insert(userCustom);

					Toast.makeText(BB_details.this, "你收藏了 :" + orderName,
							Toast.LENGTH_SHORT).show();
				}
				else
				{
					// Toast.makeText(BB_details.this, "收藏失敗! 你已經收藏了  : " +
					// orderName,Toast.LENGTH_SHORT).show();
				}

				// 點選收藏鍵後，收藏鍵無法再按
				favorite_btn.setEnabled(false);
				favorite_btn.setText("已收藏");
			}
		});

	}
//	//取得fragment2搜尋得到的民宿名稱...帶入userDao.getIdByName(String name, int position)
//	private void getDataBySearchName()
//	{
//		Bundle bundle = this.getIntent().getExtras();
//		search_name = bundle.getString("SEARCH");
//		int pos = bundle.getInt("SQL");
//		User u = userDao.getIdByName(search_name, pos);
//		
//		//以下皆為 利用User物件 帶入各自的get方法
//		String name = u.getName();
//		bb_name.setText(name);
//		
//		String address = u.getAddress();
//		bb_address.setText(address);
//		
//		String  phone = u.getPhone();
//		bb_phone.setText("電話 : " + phone);
//		
//		int price_1 = u.getPrice_1();
//		bb_price1.setText("定價 : " + price_1);
//		
//		int price_2 = u.getPrice_2();
//		bb_price2.setText("假日 : " + price_2);
//		
//		int price_3 = u.getPrice_3();
//		bb_price3.setText("平日 : " + price_3);
//
//		byte[] bit = u.getPicture();
//		Bitmap bmpout = BitmapFactory.decodeByteArray(bit, 0, bit.length);
//		bb_img.setImageBitmap(bmpout);
//		 	 
//	}

	private void initView() {
		
		//標題隱藏
		ActionBar actionBar = getActionBar();
		actionBar.hide();

		bb_img = (ImageView) findViewById(R.id.bb_img);
		bb_name = (TextView) findViewById(R.id.textView1);
		bb_address = (TextView) findViewById(R.id.textView2);
		bb_phone = (TextView) findViewById(R.id.textView3);
		bb_price1 = (TextView) findViewById(R.id.textView4);
		bb_price2 = (TextView) findViewById(R.id.textView5);
		bb_price3 = (TextView) findViewById(R.id.textView6);
		phone_btn = (Button) findViewById(R.id.call_btn);
		favorite_btn = (Button) findViewById(R.id.favorite_btn);
		map_btn = (ImageButton) findViewById(R.id.map_btn);
		
		userCustomDao = new UserCustomDao(this);
		userDao = new UserDAO(this);

		//抓取前一頁得到的id值，丟進此頁UI呈現
		getData();

		//以下判斷使用是否已收藏過該筆資料，如果有...收藏按鍵不可以再按
		orderName = userDao.getName(position);
		
		value = userCustomDao.getUserById(orderName);
		if (value) {
			favorite_btn.setEnabled(true);

		} else {
			favorite_btn.setEnabled(false);
			favorite_btn.setText("已收藏");
		}

		

	}
	
	//必須寫判斷式...同樣的屬性及UI不可重複利用佈屬..會閃退
	private void getData()
	{

		Bundle bundle = this.getIntent().getExtras();
		position = bundle.getInt("BB_POSITION");
		showDetails();
//		int values = bundle.getInt("BB_VALUES");
//		//利用從N_Activity傳過來的123 進行方法判斷
//		if(values == 123){
//			showDetails();
//		}else{
//		getDataBySearchName();
//		}
	}
	
	//這裡也可以用像寫getDataBySearchName()的寫法
	private void showDetails()
	{
		// 取得資料庫對應民宿的名稱
		name = userDao.getName(position);
		bb_name.setText(name);

		// 取得資料庫對應民宿的地址
		address = userDao.getAddress(position);
		bb_address.setText(address);

		// 取得資料庫對應民宿的電話
		phone = userDao.getPhone(position);
		bb_phone.setText("電話 : " + phone);

		// 取得資料庫對應民宿的定價
		try {
			price_1 = userDao.getPrice_1(position);
			bb_price1.setText("定價 : " + price_1);
		} catch (Exception ex) {
			ex.toString();
		}

		// 取得資料庫對應民宿的假日價
		try {
			price_2 = userDao.getPrice_2(position);
			bb_price2.setText("假日 : " + price_2);
		} catch (Exception ex) {
			ex.toString();
		}

		// 取得資料庫對應民宿的平日價
		try {
			price_3 = userDao.getPrice_3(position);
			bb_price3.setText("平日 : " + price_3);
		} catch (Exception ex) {
			ex.toString();
		}

		// 取得資料庫對應民宿的照片
		try {
			bit = userDao.getBmp(position);
			bb_img.setImageBitmap(bit);
		} catch (Exception ex) {
			ex.toString();
		}

	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.bb_details, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
